package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
/**
 * 
 * @author msjykf
 * 获取梦网直销短信状态报告接口
 *
 */

public class SmsGetRptMwzxTask implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(SmsGetRptMwzxTask.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		try {
			def timestamp = Util.getNowString("MMddHHmmss");
			PropertiesUtil pt= new PropertiesUtil();
			def userid = pt.readValue("mwzx_userid"); 
			def pwd = Util.getMD5Str(userid+pt.readValue("sms_str")+pt.readValue("mwzx_pwd")+timestamp); //userid + 固定字符串+ pwd(明文) + 时间戳，再进行MD5加密
			def ipport = pt.readValue("sms_ip")+":"+pt.readValue("mwzx_port");
			Map headMap=new HashMap(); //设置头文件
			headMap.put("Content-Type","application/json");
			JSONObject josnbody = new JSONObject();
			josnbody.put("userid",userid);
			josnbody.put("pwd",pwd);
			josnbody.put("timestamp",timestamp);
			josnbody.put("retsize",500);
			def url = "http://"+ipport+"/sms/v2/std/get_rpt";
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			def enableSSLflag = 0;
			def k =0;
			def i = 0;
			def msgjson = "";
			def sqls = "";
			def num =0;
			while(k<3) {
				msgjson = Util.doHttpPostUseHead(url,JSONObject.fromObject(josnbody).toString(),charset,timeout,enableSSLflag,headMap);
				JSONObject obj = JSONObject.fromObject(msgjson);
				//log.debug("result:"+obj.get("result"));
				if(obj.get("result")==0) {
					JSONArray rpts = obj.getJSONArray("rpts");
					//log.debug("rpts.size():"+rpts.size());
					if(rpts.size()>0) {
						k++;
					}else {
						break;
					};//获取结果为0则终止循环
					//log.debug("k:"+k);
					for (i = 0; i < rpts.size(); i++) {
						num++;
						log.debug("num:"+num);
						JSONObject it = rpts.get(i);
						//log.debug("it:"+it);
						def rstatus = it.get("status");//回执状态
						if(rstatus==0) {
							sqls = "prosperitysum=nvl(prosperitysum,0)+1";
						}else {
							sqls = "failuresum=nvl(failuresum,0)+1";
						};
						try { 
							if(it.get("exdata").equals("")) {
								continue;
							};
							eliteBean.update("update sms_send set rstate="+it.get("status")+",recode='"+it.get("errcode")+"',rtime=to_date('"+it.get("rtime")+"','yyyy-mm-dd hh24:mi:ss'),spno='"+it.get("spno")+"',pknum="+it.get("pknum")+",pktotal="+it.get("pktotal")+",rdesc='"+it.get("errdesc")+"' where smsno="+it.get("exdata")+"","dataSource");
							if(it.get("pknum")>1) {
								continue;
							};
							//log.debug("pknum=1");
							eliteBean.update("update sms_send_batch set "+sqls+" where batch_id='"+it.get("custid")+"'","dataSource");
						}catch(Exception e) {
							log.error("ErrorMessage:",e);
						}
					}
				}else {
					break;
				}
			};
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
		}
	}
}